﻿//省份相关数据
var base_provinces = new Array();
var i_prov = 0;
base_provinces[i_prov++] = new Array("100","bj","北京");
base_provinces[i_prov++] = new Array("200","gd","广东");
base_provinces[i_prov++] = new Array("210","sh","上海");
base_provinces[i_prov++] = new Array("220","tj","天津");
base_provinces[i_prov++] = new Array("230","cq","重庆");
base_provinces[i_prov++] = new Array("240","ln","辽宁");
base_provinces[i_prov++] = new Array("250","js","江苏");
base_provinces[i_prov++] = new Array("270","hb","湖北");
base_provinces[i_prov++] = new Array("280","sc","四川");
base_provinces[i_prov++] = new Array("290","sn","陕西");
base_provinces[i_prov++] = new Array("311","he","河北");
base_provinces[i_prov++] = new Array("351","sx","山西");
base_provinces[i_prov++] = new Array("371","ha","河南");
base_provinces[i_prov++] = new Array("431","jl","吉林");
base_provinces[i_prov++] = new Array("451","hl","黑龙江");
base_provinces[i_prov++] = new Array("471","nm","内蒙古");
base_provinces[i_prov++] = new Array("531","sd","山东");
base_provinces[i_prov++] = new Array("551","ah","安徽");
base_provinces[i_prov++] = new Array("571","zj","浙江");
base_provinces[i_prov++] = new Array("591","fj","福建");
base_provinces[i_prov++] = new Array("731","hn","湖南");
base_provinces[i_prov++] = new Array("771","gx","广西");
base_provinces[i_prov++] = new Array("791","jx","江西");
base_provinces[i_prov++] = new Array("851","gz","贵州");
base_provinces[i_prov++] = new Array("871","yn","云南");
base_provinces[i_prov++] = new Array("891","xz","西藏");
base_provinces[i_prov++] = new Array("898","hi","海南");
base_provinces[i_prov++] = new Array("931","gs","甘肃");
base_provinces[i_prov++] = new Array("951","nx","宁夏");
base_provinces[i_prov++] = new Array("971","qh","青海");
base_provinces[i_prov++] = new Array("991","xj","新疆");
base_provinces[i_prov++] = new Array("000","","集团");
var default_prov = new Array("210","sh","上海");

//根据省份名称(北京)、省份三位识别码(100)、省份两位简称(bj)，返回省份三位识别码
function getProvCode(sim){
	var pname = "";
	for(var i=0;i<=31;i++){
		if(base_provinces[i][0] == sim || base_provinces[i][1] == sim || base_provinces[i][2] == sim){
			pname = base_provinces[i][0];
			break;
		}
	}
	return pname;
}

//根据省份名称(北京)、省份三位识别码(100)、省份两位简称(bj)，返回省份两位简称
function getProvBrief(sim){
	var pname = "";
	for(var i=0;i<=31;i++){
		if(base_provinces[i][0] == sim || base_provinces[i][1] == sim || base_provinces[i][2] == sim){
			pname = base_provinces[i][1];
			break;
		}
	}
	return pname;
}

//根据省份名称(北京)、省份三位识别码(100)、省份两位简称(bj)，返回省份名称
function getProvName(sim){
	var pname = "";
	for(var i=0;i<=31;i++){
		if(base_provinces[i][0] == sim || base_provinces[i][1] == sim || base_provinces[i][2] == sim){
			pname = base_provinces[i][2];
			break;
		}
	}
	return pname;
}

var cur_prov_code = "";
function initProvCodeByIp() {
	cur_prov_code = "";
	var XMLHttpRequestObject = false;
	if (window.XMLHttpRequest) {
		XMLHttpRequestObject = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", "/service/ip/ip.jsp",false);
		XMLHttpRequestObject.onreadystatechange = function() {
			if (XMLHttpRequestObject.readyState == 4
				&& XMLHttpRequestObject.status == 200) {
				cur_prov_code = getProvCode(XMLHttpRequestObject.responseText.substring(0,3));
			}
		}
		XMLHttpRequestObject.send(null);
	}
}
function initProvCodeByCookie(){
	cur_prov_code = "";
	var strCookie = document.cookie;
	var arrCookie = strCookie.split("; ");
	for ( var i = 0; i < arrCookie.length; i++) {
		var arr = arrCookie[i].split("=");
		if ("CmProvid" == arr[0]) {
			cur_prov_code = getProvCode(arr[1]);
			break;
		}
	}
}
function initProvCodeByUrl(){
	cur_prov_code = "";
	var curl = document.URL;
	for(var i=0;i<=30;i++){
		if(curl.indexOf("/" + base_provinces[i][1] + "/") != -1){
			cur_prov_code = base_provinces[i][1];
			break;
		}
	}
}

//根据URL获取当前登录省份两位简称
function getCurProvCodeByUrl(){
	initProvCodeByUrl();
	return getProvCode(cur_prov_code);
}

//根据Cookie获取当前登录省份两位简称
function getCurProvCodeByCookie(){
	initProvCodeByCookie();
	return getProvCode(cur_prov_code);
}

//根据IP获取当前登录省份两位简称
function getCurProvCodeByIp(){
	initProvCodeByIp();
	return getProvCode(cur_prov_code);
}

//初始化省份数据
function initProvCode(){
	initProvCodeByUrl();//第一步：按URL地址初始化
	if(getProvCode(cur_prov_code) == "000"){
		initProvCodeByCookie();//第二步：按Cookie初始化
	}
	if(getProvCode(cur_prov_code) == "000"){
		initProvCodeByIp();//第三步：按IP初始化
	}
}


//获取当前登录省份三位代码
function getCurProvCode(){
	initProvCode();
	return getProvCode(cur_prov_code);
}

//获取当前登录省份两位简称
function getCurProvBrief(){
	initProvCode();
	return getProvBrief(cur_prov_code);
}

//获取当前登录省份名称
function getCurProvName(){
	initProvCode();
	return getProvName(cur_prov_code);
}

//获取当前登录省份三位代码，默认北京
function getCurProvCode2(){
	var rv = "";
	initProvCode();
	if(getProvCode(cur_prov_code) == "000"){
		rv = default_prov[0];
	} else {
		rv = getProvCode(cur_prov_code);
	}
	return rv;
}

//获取当前登录省份两位简称，默认北京
function getCurProvBrief2(){
	var rv = "";
	initProvCode();
	if(getProvCode(cur_prov_code) == "000"){
		rv = default_prov[1];
	} else {
		rv = getProvBrief(cur_prov_code);
	}
	return rv;
}

//获取当前登录省份名称，默认北京
function getCurProvName2(){
	var rv = "";
	initProvCode();
	if(getProvCode(cur_prov_code) == "000"){
		rv = default_prov[2];
	} else {
		rv = getProvName(cur_prov_code);
	}
	return rv;
}